package com.dingpeikeji.quickdeer.modules.wx.base.web;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.isouland.cpwx.api.base.WxOAuthAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.modules.driver.entity.TDrivers;
import com.dingpeikeji.quickdeer.core.modules.driver.service.TDriversService;
import com.dingpeikeji.quickdeer.core.modules.office.entity.SysOffice;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.modules.base.service.FrontUserService;
import com.dingpeikeji.quickdeer.modules.wx.common.web.OAuth;
import com.dingpeikeji.quickdeer.modules.wx.driver.vo.TemplateData;
import com.dingpeikeji.quickdeer.modules.wx.driver.vo.WxTemplate;
import com.dingpeikeji.quickdeer.modules.wx.util.OpenIdResult;
import com.dingpeikeji.quickdeer.modules.wx.util.TimedTask;
import com.dingpeikeji.quickdeer.modules.wx.util.TokenThread;
import com.dingpeikeji.quickdeer.modules.wx.util.WeixinUserInfo;
import com.dingpeikeji.quickdeer.modules.wx.util.WeixinUtil;
import com.dingpeikeji.quickdeer.modules.wx.wxshare.util.CommonUtil;
import com.tencent.common.Configure;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("${wxPath}/driver")
public class WeixinDriverController {

	@Autowired
	private TDriversService tDriversService;

	@RequestMapping("/toIndex")
	public String toDriverIndex(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 判断是否授权过，授权通过时，会保存session“WeixinUserInfo”，这样下次访问时，如果WeixinUserInfo存在，说明已经授权过，用户信息已经存在
		HttpSession session = request.getSession();
		TDrivers driver = (TDrivers) session.getAttribute(OAuth.SESSION_DRIVER);
		if (driver == null) {// 没有授权过，跳转授权页面，如果你不需要授权，则scope为snsapi_base，这是不会弹出授权页面
			//String url = "https://www.xiakeyueche.com/wx/driver/getOpenInfo";
			String url = TimedTask.websiteAndProject+"/driver/getOpenInfo";
			String ipath = URLEncoder.encode(url, "utf-8");
			//测试  appid = timetask.appid
			String iurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + TimedTask.appid
			//String iurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Configure.getAppid()
					+ "&redirect_uri=" + ipath
					+ "&response_type=code&scope=snsapi_base&state=driver#wechat_redirect";
			return "redirect:" + iurl;
		} else {
			return "redirect:" + Global.getWxPath() + "/d/realschevehi/taskList";
		}
	}

	@RequestMapping("/getOpenInfo")
	public String getOpenInfo(@RequestParam("code") String code, @RequestParam("state") String state,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 用户同意授权
		String openid = "";
		// 获取OpenId
		if (StringUtils.isNotEmpty(code)) {
			openid = WxOAuthAPI.getOpenid(TimedTask.appid, TimedTask.appsecret, code);
			//openid = WxOAuthAPI.getOpenid(Configure.getAppid(), Configure.getAppsecret(), code);
		}
		if (!StringUtils.isEmpty(openid) && state.equals("driver")) {
			TDrivers driver = (TDrivers) request.getSession().getAttribute(OAuth.SESSION_DRIVER);
			if (driver == null) {
				// 身份认证
				TDrivers query = new TDrivers();
				query.setWechatid(openid);
				List<TDrivers> driverList = tDriversService.findList(query);
				if (driverList != null && !driverList.isEmpty()) { // 认证通过
					request.getSession().setAttribute(OAuth.SESSION_DRIVER, driverList.get(0));
					return "redirect:" + Global.getWxPath() + "/d/realschevehi/taskList";
				} else { // 去绑定
					return "redirect:" + Global.getWxPath() + "/bind?openid=" + openid;
				}
			} else {
				return "redirect:" + Global.getWxPath() + "/d/realschevehi/taskList";
			}
		}
		return "wx/modules/base/index";
	}
	
}



















